package com.boyuan.huijiaoyu;

import com.easemob.chat.EMChatManager;
import com.magicsoft.weitown.ui.CustomDialog;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class LogoutActivity extends Activity implements OnClickListener{

	private String mobile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logout);
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		mobile = sp.getString("username", "");
		initUI();
	}

	private void initUI() {
		findViewById(R.id.lay_outuser).setOnClickListener(this);
		findViewById(R.id.lay_outapp).setOnClickListener(this);
	}
	
	private void logout() {

		final CustomDialog dialog = new CustomDialog(LogoutActivity.this,
				R.style.qr_dialog);
		dialog.show();
		// 标题
		TextView tv_title = (TextView) dialog.findViewById(R.id.dialog_title);
		tv_title.setText("确定退出");
		// 内容
		TextView tv_content = (TextView) dialog
				.findViewById(R.id.dialog_content);
		tv_content.setVisibility(View.INVISIBLE);
		tv_content.setText("");
		// 右上角取消
		ImageView imag_cancel = (ImageView) dialog
				.findViewById(R.id.dialog_img_cancel);
		imag_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		// 确定按钮
		Button btn_ok = (Button) dialog.findViewById(R.id.dialog_button_ok);
		btn_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				dialog.cancel();
				SharedPreferences sp3 = getSharedPreferences(
						"user.xml", 0);
				Editor edit = sp3.edit();
				edit.putBoolean("islogin", false);
				edit.commit();
				EMChatManager.getInstance().logout();
//				((MainActivity) getActivity()).popupLogin();
				Intent data = new Intent();
				setResult(2, data );
				Intent intent = new Intent(LogoutActivity.this, AccountLoginActivity.class);
				intent.putExtra("mobile", mobile);
				startActivity(intent);
				finish();
			}
		});
		// 取消按钮
		Button btn_cancel = (Button) dialog
				.findViewById(R.id.dialog_button_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.logout, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_outuser:
			exitUser();
			break;
		case R.id.lay_outapp:
			exitApp();
			break;
		default:
			break;
		}
	}
	
	private void exitApp() {
		exit();
	}

	private void exit() {
		final CustomDialog dialog = new CustomDialog(LogoutActivity.this,
				R.style.qr_dialog);
		dialog.show();
		// 标题
		TextView tv_title = (TextView) dialog.findViewById(R.id.dialog_title);
		tv_title.setText("确定退出");
		// 内容
		TextView tv_content = (TextView) dialog
				.findViewById(R.id.dialog_content);
		tv_content.setVisibility(View.INVISIBLE);
		tv_content.setText("");
		// 右上角取消
		ImageView imag_cancel = (ImageView) dialog
				.findViewById(R.id.dialog_img_cancel);
		imag_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		// 确定按钮
		Button btn_ok = (Button) dialog.findViewById(R.id.dialog_button_ok);
		btn_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				dialog.cancel();
				SharedPreferences sp3 = getSharedPreferences(
						"user.xml", 0);
				Editor edit = sp3.edit();
				edit.putBoolean("islogin", false);
				edit.commit();
				EMChatManager.getInstance().logout();
//				((MainActivity) getActivity()).popupLogin();
				Intent data = new Intent();
				setResult(1, data );
				finish();
			}
		});
		// 取消按钮
		Button btn_cancel = (Button) dialog
				.findViewById(R.id.dialog_button_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
	}

	private void exitUser() {
		logout();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return super.onTouchEvent(event);
	}

}
